package com.huawei.fastengine.fastview;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import com.huawei.fastengine.R;
import com.huawei.fastengine.fastview.download.download.HiappDownloadActivity;
import com.huawei.fastengine.fastview.download.utils.CheckUtils;
import com.huawei.fastengine.fastview.download.utils.PackageUtils;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import com.huawei.fastengine.fastview.startFastappEngine.JumpActivity;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import java.lang.ref.WeakReference;
import o.eye;
import o.eye.d;

public class StartFastAPPEngine {
    public static final String ACTION_AIDL_FASTAPP = "com.huawei.fastapp.engine.action.AIDL_SERVICE";
    private static final String ACTION_MANAGEMENT_FASTAPP = "com.huawei.fastapp.ACTION_MANAGER";
    private static final String TAG = "StartFastAPPEngine";
    private static StartFastAPPEngine mInstance = new StartFastAPPEngine();
    private ServiceConnection conn = new ServiceConnection() {
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Message obtain = Message.obtain(StartFastAPPEngine.this.mHandler);
            obtain.what = 1002;
            StartFastAPPEngine.this.mHandler.sendMessage(obtain);
            StartFastAPPEngine.this.fastappService = d.ˎ(iBinder);
            try {
                FastViewLogUtils.d(StartFastAPPEngine.TAG, " onServiceConnected bind success");
                if (StartFastAPPEngine.this.mBundle != null) {
                    FastViewLogUtils.i(StartFastAPPEngine.TAG, " onServiceConnected startManagerment");
                    StartFastAPPEngine.this.fastappService.ˎ(StartFastAPPEngine.this.mBundle.getString("channel"), StartFastAPPEngine.this.mBundle.getString("target"), null);
                } else if (StartFastAPPEngine.this.mRpkinfo != null) {
                    FastViewLogUtils.d(StartFastAPPEngine.TAG, " onServiceConnected startRpk");
                    StartFastAPPEngine.this.fastappService.ˊ(StartFastAPPEngine.this.mRpkinfo.getPackageName(), StartFastAPPEngine.this.mRpkinfo.getAppid(), StartFastAPPEngine.this.mRpkinfo.getDownloadUrl(), StartFastAPPEngine.this.mRpkinfo.getVersionCode(), StartFastAPPEngine.this.mRpkinfo.getDigest(), null);
                }
                StartFastAPPEngine.this.unbindService();
            } catch (RemoteException e) {
                FastViewLogUtils.d(StartFastAPPEngine.TAG, " ERROR RemoteException");
            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            FastViewLogUtils.e(StartFastAPPEngine.TAG, "onServiceDisconnected");
            StartFastAPPEngine.this.fastappService = null;
        }
    };
    private AlertDialog dialog;
    private eye fastappService;
    private boolean isNeedStartActivity = false;
    private Bundle mBundle;
    private WeakReference<Context> mContextWr;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message message) {
            FastViewLogUtils.e(StartFastAPPEngine.TAG, "handleMessage: " + message.what);
            Intent intent;
            switch (message.what) {
                case 1001:
                    Value value = (Value) message.obj;
                    StartFastAPPEngine.this.mRpkinfo = value.mRpkInfoValue;
                    StartFastAPPEngine.this.mContextWr = new WeakReference(value.mContext);
                    if (PackageUtils.isInstallByPackage(value.mContext, Config.FASTAPP_PACKAGE_NAME)) {
                        StartFastAPPEngine.this.bindService(value.mContext);
                        return;
                    }
                    intent = new Intent(value.mContext, HiappDownloadActivity.class);
                    intent.putExtra("packageName", StartFastAPPEngine.this.mRpkinfo.getPackageName());
                    value.mContext.startActivity(intent);
                    return;
                case 1002:
                    return;
                case 1003:
                    FastViewLogUtils.e(StartFastAPPEngine.TAG, "BIND_FAILED: startActivity");
                    if (StartFastAPPEngine.this.mContextWr != null) {
                        Context context = (Context) StartFastAPPEngine.this.mContextWr.get();
                        if (context != null) {
                            intent = new Intent();
                            intent.setClass(context, JumpActivity.class);
                            intent.putExtra("isfromold", true);
                            if (!(context instanceof Activity)) {
                                intent.setFlags(268435456);
                            }
                            try {
                                context.startActivity(intent);
                                if (context instanceof Activity) {
                                    ((Activity) context).overridePendingTransition(0, 0);
                                    return;
                                }
                                return;
                            } catch (ActivityNotFoundException e) {
                                FastViewLogUtils.e(StartFastAPPEngine.TAG, "ActivityNotFoundException：");
                                return;
                            }
                        }
                        return;
                    }
                    return;
                case 1004:
                    StartFastAPPEngine.this.mTimeOut = false;
                    return;
                default:
                    FastViewLogUtils.e(StartFastAPPEngine.TAG, "error default ");
                    return;
            }
        }
    };
    private RpkInfo mRpkinfo;
    private boolean mTimeOut = false;

    static class Value {
        public Context mContext;
        public RpkInfo mRpkInfoValue;

        public Value(Context context, RpkInfo rpkInfo) {
            this.mContext = context;
            this.mRpkInfoValue = rpkInfo;
        }
    }

    private StartFastAPPEngine() {
    }

    private void bindService(Context context) {
        FastViewLogUtils.d(TAG, "begin to bindService");
        if (context == null) {
            FastViewLogUtils.e(TAG, "bindService context is null");
        } else if (!CheckUtils.isCorrectSign(context, Config.FASTAPP_PACKAGE_NAME)) {
            FastViewLogUtils.e(TAG, "FastAPP sign is Wrong");
        } else if (this.isNeedStartActivity) {
            this.isNeedStartActivity = false;
            FastViewLogUtils.e(TAG, "firstStart");
            r0 = Message.obtain(this.mHandler);
            r0.what = 1003;
            this.mHandler.sendMessage(r0);
        } else {
            Intent intent = new Intent();
            intent.setAction("com.huawei.fastapp.engine.action.AIDL_SERVICE");
            intent.setPackage(Config.FASTAPP_PACKAGE_NAME);
            if (!context.bindService(intent, this.conn, 1)) {
                FastViewLogUtils.e(TAG, "bind service failed");
                r0 = Message.obtain(this.mHandler);
                r0.what = 1003;
                this.mHandler.sendMessage(r0);
            }
        }
    }

    public static StartFastAPPEngine getInstance() {
        return mInstance;
    }

    private void showNormalDialog(Context context) {
        if (context != null) {
            CharSequence string = context.getString(R.string.fastview_sdk_fastapp_noopen_msg);
            Builder builder = new Builder(context);
            builder.setMessage(string);
            builder.setPositiveButton(context.getString(R.string.fastview_sdk_fastapp_aidl_confirm), null);
            this.dialog = builder.create();
            this.dialog.setCanceledOnTouchOutside(false);
            if ((context instanceof Activity) && !((Activity) context).isFinishing() && !((Activity) context).isDestroyed()) {
                this.dialog.show();
            }
        }
    }

    private void startManagermentActivity(Context context, Bundle bundle) {
        Intent intent = new Intent();
        String string = bundle.getString("channel");
        String string2 = bundle.getString("target");
        intent.setAction(ACTION_MANAGEMENT_FASTAPP);
        intent.setPackage(Config.FASTAPP_PACKAGE_NAME);
        intent.putExtra("channel", string);
        intent.putExtra("target", string2);
        try {
            if (context instanceof Activity) {
                ((Activity) context).startActivity(intent);
                return;
            }
            intent.addFlags(268435456);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            FastViewLogUtils.e(TAG, "openFastappManagement ActivityNotFoundException!");
        }
    }

    private void unbindService() {
        FastViewLogUtils.e("", "unbind Service");
        try {
            if (this.mContextWr != null) {
                Context context = (Context) this.mContextWr.get();
                if (context != null) {
                    context.unbindService(this.conn);
                }
            }
        } catch (Exception e) {
            FastViewLogUtils.i("", "unbind service error");
        }
    }

    public void openFastApp(Context context, RpkInfo rpkInfo) {
        if (rpkInfo != null && !this.mTimeOut) {
            this.mTimeOut = true;
            this.isNeedStartActivity = false;
            this.mBundle = null;
            FastViewLogUtils.e(TAG, "openFastApp");
            Value value = new Value(context, rpkInfo);
            Message obtain = Message.obtain(this.mHandler);
            obtain.what = 1001;
            obtain.obj = value;
            this.mHandler.removeMessages(1001);
            this.mHandler.sendMessage(obtain);
            Message obtain2 = Message.obtain(this.mHandler);
            obtain2.what = 1004;
            this.mHandler.sendMessageDelayed(obtain2, 500);
        }
    }

    public void openFastAppFromDownload(RpkInfo rpkInfo) {
        if (this.mContextWr != null) {
            Context context = (Context) this.mContextWr.get();
            if (context != null) {
                this.isNeedStartActivity = true;
                this.mBundle = null;
                FastViewLogUtils.e(TAG, "openFastApp first");
                Value value = new Value(context, rpkInfo);
                Message obtain = Message.obtain(this.mHandler);
                obtain.what = 1001;
                obtain.obj = value;
                this.mHandler.sendMessage(obtain);
            }
        }
    }

    public void openFastappManagement(Context context, Bundle bundle) {
        if (context == null || bundle == null) {
            FastViewLogUtils.e(TAG, "openFastappManagement input null!");
            return;
        }
        this.mContextWr = new WeakReference(context);
        this.mBundle = bundle;
        this.isNeedStartActivity = false;
        if (PackageUtils.isEngineInstalled(context)) {
            startManagermentActivity(context, bundle);
            return;
        }
        Intent intent = new Intent(context, HiappDownloadActivity.class);
        intent.putExtra("Management", bundle);
        context.startActivity(intent);
    }

    public void openFastappManagementFromDownload(Bundle bundle) {
        if (this.mContextWr != null) {
            Context context = (Context) this.mContextWr.get();
            if (context == null || bundle == null) {
                FastViewLogUtils.e(TAG, "openFastappManagementFromDownload null!");
                return;
            }
            this.mBundle = bundle;
            bindService(context);
        }
    }

    public void setJumpActivityStatus(boolean z) {
        FastViewLogUtils.d(TAG, "setActivityStatus: " + z);
        if (this.mContextWr != null) {
            if (z) {
                bindService((Context) this.mContextWr.get());
            } else if (this.dialog == null) {
                showNormalDialog((Context) this.mContextWr.get());
            } else if (!this.dialog.isShowing()) {
                showNormalDialog((Context) this.mContextWr.get());
            }
        }
    }
}
